import axios, { type AxiosResponse } from 'axios'
// import { useAuthStore, useUserStore } from '@/stores'
// import { useDispatch, useSelector } from 'react-redux'
import token1 from '@/api/token'
const service = axios.create({
  baseURL: import.meta.env.VITE_GLOB_API_URL,
  // timeout: 2 * 60000,
})

service.interceptors.request.use(
  (config) => {
    const userId = 356
    // 给接口增加user_Id 临时用
    // 1. post 请求修改
    if(config.method === 'post') {
      if(config.data){
        config.data.user_id = userId
      }
    }
    // 2. get请求修改
    else {
      if(userId) {
        if(config.params) {
          config.params.user_id = userId
        }
      }
    }
    // const token = useAuthStore().token
    const token = localStorage.getItem('token')
    if (token)
      config.headers.Authorization = `Bearer ${localStorage.getItem("react_token")}`
    return config
  },
  (error) => {
    return Promise.reject(error.response)
  },
)

service.interceptors.response.use(
  (response: AxiosResponse): AxiosResponse => {
    if (response.status === 200)
      return response

    throw new Error(response.status.toString())
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default service
